package com.hivekion.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hivekion.common.entity.PagedResultVo;
import com.hivekion.customer.domain.Customer;
import com.hivekion.customer.domain.vo.CustomerModelVo;
import com.hivekion.customer.domain.vo.CustomerSearchInputVo;
import com.hivekion.customer.domain.vo.CustomerViewVo;
import com.hivekion.customer.mapper.CustomerMapper;
import com.hivekion.customer.service.ICustomerService;
import com.hivekion.system.domain.SysOperationLog;
import com.hivekion.system.domain.vo.SysOperationLogViewVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户管理服务类
 */

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Resource
    private CustomerMapper mapper;


    @Override
    public PagedResultVo<CustomerViewVo> getList(CustomerSearchInputVo vo) {
        LambdaQueryWrapper<Customer> logLambdaQueryWrapper = Wrappers.lambdaQuery();
        logLambdaQueryWrapper.eq(StringUtils.isNotBlank(vo.getNickName()), Customer::getNickName, vo.getNickName())
                .eq(StringUtils.isNotBlank(vo.getEmail()), Customer::getEmail, vo.getEmail())
                .eq(StringUtils.isNotBlank(vo.getPhoneNumber()), Customer::getPhoneNumber, vo.getPhoneNumber())
                .eq(ObjectUtils.isNotEmpty(vo.getStatus()), Customer::getStatus, vo.getStatus());
        Page<Customer> logPage = new Page<>(vo.getPageNum(), vo.getPageSize());
        logPage.addOrder(new OrderItem("create_time", false));
        IPage<Customer> logPageResult = mapper.selectPage(logPage, logLambdaQueryWrapper);
        List<CustomerViewVo> data =
                logPageResult.getRecords().stream()
                        .map(
                                item -> {
                                    CustomerViewVo v = new CustomerViewVo();
                                    BeanUtils.copyProperties(item, v);
                                    return v;
                                })
                        .collect(Collectors.toList());
        PagedResultVo<CustomerViewVo> resultVo =
                new PagedResultVo<CustomerViewVo>(
                        vo, (int) logPageResult.getTotal(), data);
        return resultVo;
    }

    @Override
    public boolean updateStatus(String key, Integer status) {
        Customer customer = mapper.selectById(key);
        if (ObjectUtils.isNotEmpty(customer)) {
            customer.setStatus(status);
            mapper.updateById(customer);
        } else {
            return false;
        }
        return true;
    }

    @Override
    public CustomerModelVo getInfo(String key) {
        Customer customer = mapper.selectById(key);
        if (ObjectUtils.isNotEmpty(customer)) {
            CustomerModelVo vo = new CustomerModelVo();
            BeanUtils.copyProperties(customer, vo);
            return vo;
        }
        return null;
    }
}
